{% extends "base/base.html" %}

{% block title %}按客户统计 - 项目合作归属数据管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <h2><i class="bi bi-people"></i> 按客户统计</h2>
        <p class="text-muted">根据客户统计商谈记录和合作情况</p>
    </div>
</div>

<!-- 搜索表单 -->
<div class="search-form">
    <form method="GET" class="row g-3">
        <div class="col-md-4">
            <label class="form-label">客户姓名 <span class="text-danger">*</span></label>
            <select class="form-select" name="customer_name" required>
                <option value="">请选择客户</option>
                {% for customer in customer_names %}
                <option value="{{ customer }}" {% if customer_name == customer %}selected{% endif %}>{{ customer }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label">开始时间 <span class="text-danger">*</span></label>
            <input type="date" class="form-control" name="start_date" value="{{ start_date }}" required>
        </div>
        <div class="col-md-3">
            <label class="form-label">结束时间 <span class="text-danger">*</span></label>
            <input type="date" class="form-control" name="end_date" value="{{ end_date }}" required>
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-search"></i> 查询
            </button>
        </div>
    </form>
</div>

<!-- 统计结果表格 -->
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">统计结果</h5>
        <button class="btn btn-outline-primary btn-sm" onclick="exportStats()">
            <i class="bi bi-download"></i> 导出统计
        </button>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>客户姓名</th>
                        <th>线上商谈总时长</th>
                        <th>线上商谈次数</th>
                        <th>线上平均时长</th>
                        <th>线下商谈总时长</th>
                        <th>线下商谈次数</th>
                        <th>线下平均时长</th>
                        <th>累计总时长</th>
                        <th>累计总次数</th>
                        <th>平均时长</th>
                        <th>平均每天商谈次数</th>
                        <th>统计天数</th>
                    </tr>
                </thead>
                <tbody>
                    {% for result in results %}
                    <tr>
                        <td>{{ result.customer_name }}</td>
                        <td>{{ "%.1f"|format(result.online_total_duration / 60) }}小时</td>
                        <td>{{ result.online_count }}次</td>
                        <td>{{ "%.1f"|format(result.online_avg_duration / 60) }}小时</td>
                        <td>{{ "%.1f"|format(result.offline_total_duration / 60) }}小时</td>
                        <td>{{ result.offline_count }}次</td>
                        <td>{{ "%.1f"|format(result.offline_avg_duration / 60) }}小时</td>
                        <td>{{ "%.1f"|format(result.total_duration / 60) }}小时</td>
                        <td>{{ result.total_count }}次</td>
                        <td>{{ "%.1f"|format(result.total_avg_duration / 60) }}小时</td>
                        <td>{{ "%.2f"|format(result.avg_daily_negotiations) }}次/天</td>
                        <td>{{ result.time_period_days }}天</td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="12" class="text-center text-muted py-4">
                            {% if customer_name and start_date and end_date %}
                                暂无统计数据
                            {% else %}
                                请选择查询条件后点击查询按钮
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function exportStats() {
    // 导出统计数据
    const params = new URLSearchParams(window.location.search);
    const exportUrl = '{{ url_for("statistics.export_stats_by_customer") }}?' + params.toString();
    window.location.href = exportUrl;
}
</script>
{% endblock %}
